package cn.lan.scau_hotel_service.controller;

import cn.lan.scau_hotel_service.entity.User;
import cn.lan.scau_hotel_service.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "添加和更新user",notes = "添加不需要有id这一项，更新需要有user的id;返回的是添加或者更新后的结果")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @RequestMapping(value="", method=RequestMethod.POST)
    public User addAndUpdateUser(@RequestBody User user) {
        System.out.println("***********************************************************************"+user.getOpenid());
        System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"+user.getVIP());
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + user.getUserPhone());
        if(userService.getUserByOpenId(user.getOpenid())!=null){
            User haveuser = userService.getUserByOpenId(user.getOpenid());
            haveuser.setVIP(user.getVIP());
            haveuser.setUserPhone(user.getUserPhone());
            userService.saveUser(haveuser);
        }else{

            userService.saveUser(user);
        }
        int id = userService.getUserByOpenId(user.getOpenid()).getId();
        return userService.getUserById(id);
    }

    @RequestMapping(value = "/list" ,method=RequestMethod.GET)
    public List<User> getUserList(){
        return userService.getOrders();
    }

    @RequestMapping(value="", method=RequestMethod.DELETE)
    public String deleteUser(@RequestBody User user) {
        int id = user.getId();
        return "删除结果：" + userService.deleteUserById(id);
    }

    @RequestMapping(value="/{id}", method= RequestMethod.DELETE)
    public String deleteUserByOpenId(@PathVariable int id) {
        return "删除结果：" + userService.deleteUserById(id);
    }

    @ApiOperation(value="获取用户对象", notes="根据url的openid来获取用户详细信息,如果数据库中不存在，" +
            "则会添加这个用户；返回的对象数据中有id这个数据，之后的一切操作都用id,不要用openid")
    @ApiImplicitParam(name = "openid", value = "用户openid", required = true, dataType = "String")
    @RequestMapping(value="/{openid}", method= RequestMethod.GET)
    public User getUserByOpenId(@PathVariable String openid) {
        System.out.println("--------------------------openid:" + openid );
        if(userService.getUserByOpenId(openid)!=null){
            User user = userService.getUserByOpenId(openid);
            System.out.println("------------------------" + "已存在");
            return user;
        }else{
            System.out.println("------------------------" + "不存在");
            User user = new User();
            user.setOpenid(openid);
            userService.saveUser(user);

            return  userService.getUserByOpenId(openid);
        }
    }
}
